package com.hellobike.haxing.modules.dealcampaign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.dealcampaign.entity.DealCampaign;
import com.hellobike.haxing.modules.dealcampaign.mapper.DealCampaignMapper;
import com.hellobike.haxing.modules.dealcampaign.service.IDealCampaignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: t_deal_campaign
 * @Author: haxing-boot
 * @Date: 2020-02-28
 * @Version: V1.0
 */
@Service
public class DealCampaignServiceImpl extends ServiceImpl<DealCampaignMapper, DealCampaign> implements IDealCampaignService {

    @Autowired
    private DealCampaignMapper dealCampaignMapper;

    @Override
    public Result saveDealCampaign(DealCampaign dealCampaign) {
        if (!checkUniqueActive(dealCampaign)) {
            return Result.error("只能有一个活动为开启状态");
        }
        dealCampaignMapper.insert(dealCampaign);
        return Result.ok("添加成功！");
    }

    @Override
    public Result updateDealCampaign(DealCampaign dealCampaign) {
        if (!checkUniqueActive(dealCampaign)) {
            return Result.error("只能有一个活动为开启状态");
        }
        dealCampaignMapper.updateById(dealCampaign);
        return Result.ok("编辑成功！");
    }

    @Override
    public Result<?> changeState(Long id, Integer status) {
        if (status == 0) {
            QueryWrapper<DealCampaign> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(DealCampaign::getStatus, 0);
            List<DealCampaign> activeDealCampaigns = dealCampaignMapper.selectList(queryWrapper);
            boolean checked = true;
            if (activeDealCampaigns.size() > 1) {
                checked = false;
            } else if (activeDealCampaigns.size() == 1) {
                checked = id.equals(activeDealCampaigns.get(0).getId());
            }
            if (!checked) {
                return Result.error("同时只允许开启一个活动");
            }
        }
        DealCampaign dealCampaign = new DealCampaign();
        dealCampaign.setId(id);
        dealCampaign.setStatus(status);
        dealCampaignMapper.updateById(dealCampaign);
        return Result.ok("状态修改成功");
    }

    //检查是否唯一开启的
    private boolean checkUniqueActive(DealCampaign dealCampaign) {
        if (dealCampaign.getStatus() == 0) {
            QueryWrapper<DealCampaign> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(DealCampaign::getStatus, 0);
            List<DealCampaign> activeDealCampaigns = dealCampaignMapper.selectList(queryWrapper);
            if (activeDealCampaigns.size() > 1) {
                return false;
            } else if (activeDealCampaigns.size() == 1) {
                if (dealCampaign.getId() == null) {
                    return false;
                } else {
                    return dealCampaign.getId().equals(activeDealCampaigns.get(0).getId());
                }
            } else {
                return true;
            }
        }
        return true;
    }
}
